package cn.ahpu.tools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class FileOperation {
	public static void wireFile(String fileName,String id,String[] equation) {
		File file = new File(fileName);
		//判断文件是否存在
		if(!file.exists()) {
			System.out.println("文件已创建！");
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			FileOutputStream fos = new FileOutputStream(fileName);
			OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
			BufferedWriter bw = new BufferedWriter(osw);
			PrintWriter pw = new PrintWriter(bw,true);
			
			pw.println("id:"+id);
			for (int i = 0; i < equation.length; i++) {
				String line = equation[i];
				pw.println(line);
			}
			
			pw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void readFile(String fileName) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(fileName);
			InputStreamReader isr = new InputStreamReader(fis,"gbk");
			BufferedReader br = new BufferedReader(isr);
			
			String line = null;
			while((line = br.readLine())!=null) {
				System.out.println(line);
			}
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

//	public static void main(String[] args) {
//		String fileName = "result.txt";
//		String[] s = {"aasd123sd","a2sd"};
//		FileOperation.wireFile(fileName,"asd",s);
//		FileOperation.readFile(fileName);
//	}
}
